package com.nitty.cash.loop.ui.presenter

import com.lib.common.permission.CommonPermissions
import com.nitty.cash.loop.common.CommonPresenter
import com.nitty.cash.loop.configs.NittyApiPath
import com.nitty.cash.loop.configs.NittyApiUrl
import com.nitty.cash.loop.configs.NittyApiUrl.addCommonParams
import com.nitty.cash.loop.request.IHttpCallback
import com.nitty.cash.loop.ui.contract.IViewContract
import com.nitty.cash.loop.utils.helper.NittyConfigsHelper
import com.nitty.cash.loop.utils.helper.NittyCreditStatusHelper
import com.nitty.cash.loop.utils.helper.NittyProcessHelper
import com.nitty.cash.loop.utils.helper.TestDataLibInvoke
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey
import org.json.JSONObject

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/9/10 13:41
 * @Annotation：额度审核中
 */
class LimitReviewingPresenter(private val iView: IViewContract.ILimitReviewingView): CommonPresenter(iView) {
    /**
     * 获取授信状态
     */
    fun queryNittyCreditStatusAction(){
        iView.hiddenNittyLoading()
        val isCreditReviewing = NittyCreditStatusHelper.NittyCreditStatusRepository().isCreditReviewing()
        //授信中
        if (isCreditReviewing){
            iView.showLimitReviewingViews()
            return
        }

        val creditStatus = NittyProcessHelper.getAuthBeforeRiskStatus()
        val isCreditRejected = NittyCreditStatusHelper.NittyCreditStatusRepository().isCreditRejected(creditStatus)
        //授信被拒绝
        if (isCreditRejected){
            iView.showLimitFailedViews()
            return
        }
        //授信完成
        getNittyWithdrawAmountAction()
    }

    /**
     * 获取额度信息
     */
    private fun getNittyWithdrawAmountAction(){
        val httpUrl = NittyApiUrl.getHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("xd304965bcd9478080", NittyApiPath.NITTY_PATH.NITTY_API_LOAN_LIMIT_PATH)
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    iView.showLimitSuccessViews(obj)
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenNittyLoading()
            }
        })
    }

    /**
     * 检查权限是否全部申请
     */
    fun checkNittyLimitReviewingPermissionsAgree(){
        //是否是审核账号
        val isConfigAccount = NittySharedHelper.getNittySharedBoolean(NittySharedKey.NITTY_IS_CONFIG_ACCOUNT)
        if (isConfigAccount) {//审核账号，可以进行申贷操作
            showNittyLimitReviewingPermissionSuccess()
            return
        }

        val permissions = NittyConfigsHelper.getNittyPermissionsList()
        if (CommonPermissions.isGranted(iView.getAct(),permissions)){
            showNittyLimitReviewingPermissionSuccess()
            return
        }
        //权限未申请
        iView.showLimitPermissionPage()
    }

    /**
     * 权限检查成功
     */
    private fun showNittyLimitReviewingPermissionSuccess(){
        //上报
        TestDataLibInvoke.sycData()
        iView.showLimitApplyPage()
    }
}